package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.CollectAdapter;
import com.wnhz.work.jy.bean.CollectBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---收藏
 */
public class CollectActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private static final String TAG = "CollectActivity";
    private TextView title;
    private ListView lv_collect;

    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;

    private CollectAdapter mAdapter;
    private List<CollectBean> collectList = new ArrayList<>();

    private String[] titles = {"2016年熊猫普制金币30克", "2016年熊猫普制金币40克", "2016年熊猫普制金币60克"};
    private String[] prices = {"¥ 9071.00", "¥ 12071.00", "¥ 17071.00"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);

        x.view().inject(this);
        //initData();
        initView();
        setBlank();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void setBlank() {
        if (collectList.size() == 0) {
            blank_tv.setText("快去挑个心怡的商品收藏吧~");
            blank_img.setImageResource(R.mipmap.ic_kongshoucang);
            include_occupied.setVisibility(View.VISIBLE);
        } else {
            include_occupied.setVisibility(View.GONE);
        }
    }


    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("收藏夹");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        lv_collect = (ListView) findViewById(R.id.lv_collect);

        mAdapter = new CollectAdapter(CollectActivity.this, collectList, this);
        lv_collect.setAdapter(mAdapter);
        lv_collect.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //取消收藏
            case R.id.tv_cancel:
                int index = (int) v.getTag();
                cancelCollection(collectList.get(index).getGoodId());
                break;

            default:
                break;
        }
    }

    private void cancelCollection(String goodId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("goodId", goodId);
        showDialog();
        XUtil.Post(ConfigUtil.CANCEL_MY_COLLECTION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject js = new JSONObject(result);
                    String info = js.optString("info");
                    MyToast(info);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                getData();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.MY_COLLECTION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.d(TAG, "onSuccess: " + result);
                try {
                    JSONObject js = new JSONObject(result);
                    String re = js.optString("result");
                    collectList.clear();
                    if ("1".equals(re)) {
                        JSONArray array = js.optJSONArray("info");
                        Gson gson = new Gson();

                        if (array == null) {
                            mAdapter.notifyDataSetChanged();
                            setBlank();
                            return;
                        }
                        for (int i = 0; i < array.length(); i++) {
                            CollectBean bean = new CollectBean();
                            bean = gson.fromJson(array.optJSONObject(i).toString(), CollectBean.class);
                            collectList.add(bean);
                        }
                    } else {

                    }
                    mAdapter.notifyDataSetChanged();
                    setBlank();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                Log.e(TAG, "onError: " + ex);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, GoodDetailsMainActivity.class);
        intent.putExtra("goodId", collectList.get(position).getGoodId());
        startActivity(intent);
    }
}
